package com.yystar.support.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Web Application Utility
 * 
 * @author Worm Yung
 * @version 1.0 (2007-Nov-08)
 * 
 *          Revision 2007-Nov-08 First Production
 */
public class AppUtility {
	private static Log exceptionLog = LogFactory.getLog("ExceptionLog");

	// ---------------------------------------------------------------------------------------
	// Static Methods

	/**
     * 
     */
	public static void recordException(Exception ex) {
		// System.err.println(AppInfo.APP_NAME + " " + AppInfo.VERSION + " " +
		// ClockService.getInstance().getDatabaseDateTime());
		ex.printStackTrace();
		exceptionLog.error(ex.getMessage(), ex);
	}

	/**
	 * 
	 * @param errorMessage
	 */
	public static void recordErrorMessage(String errorMessage) {
		// System.err.println(AppInfo.APP_NAME + " " + AppInfo.VERSION + " " +
		// ClockService.getInstance().getDatabaseDateTime() + "\t" +
		// errorMessage);
		exceptionLog.error(errorMessage);
	}

	/*
	 * the loadServerConfig() method is load Server config
	 */
	public static void loadServerConfig() {
		String path = System.getProperty("catalina.base") + "/conf/";
		String fileName = "sb2.conf";
		String defaultValue = "0";
		String enabledValue = "1";
		String disabledValue = "0";
		String opServer = "op_server";
		String agServer = "ag_server";
		String meServer = "me_server";
		String retailServer = "retail_server";
		String telebetServer = "telebet_server";
		String apgService = "apg_service";
		String interfaceServer = "interface_server";
		String comment = "sb2 server config. 1: Enable, 0: Disable";
		String oldPath = "/conf/";

		File oldConfigFile = new File(oldPath + fileName);
		Properties props = new Properties();
		File configFile = new File(path + fileName);
		InputStream configInputStream = null;
		OutputStream configOutputStream = null;

		try {
			// Set config value
			if (configFile.exists()) {
				configInputStream = new FileInputStream(configFile);
				props.load(configInputStream);

				AppConfig.isOpServerEnabled = props.getProperty(opServer, defaultValue).equals(enabledValue);
				AppConfig.isAgServerEnabled = props.getProperty(agServer, defaultValue).equals(enabledValue);
				AppConfig.isMeServerEnabled = props.getProperty(meServer, defaultValue).equals(enabledValue);
				AppConfig.isRetailServerEnabled = props.getProperty(retailServer, defaultValue).equals(enabledValue);
				AppConfig.isTelebetServerEnabled = props.getProperty(telebetServer, defaultValue).equals(enabledValue);
				AppConfig.isAPGServiceEnabled = props.getProperty(apgService, defaultValue).equals(enabledValue);
				AppConfig.isInterfaceServerEnabled = props.getProperty(interfaceServer, defaultValue).equals(enabledValue);
			} else {
				if (oldConfigFile.exists()) {
					configInputStream = new FileInputStream(oldConfigFile);
					props.load(configInputStream);
					AppConfig.isOpServerEnabled = props.getProperty(opServer, defaultValue).equals(enabledValue);
					AppConfig.isAgServerEnabled = props.getProperty(agServer, defaultValue).equals(enabledValue);
					AppConfig.isMeServerEnabled = props.getProperty(meServer, defaultValue).equals(enabledValue);
					AppConfig.isRetailServerEnabled = props.getProperty(retailServer, defaultValue).equals(enabledValue);
					AppConfig.isTelebetServerEnabled = props.getProperty(telebetServer, defaultValue).equals(enabledValue);
					AppConfig.isAPGServiceEnabled = props.getProperty(apgService, defaultValue).equals(enabledValue);
					AppConfig.isInterfaceServerEnabled = props.getProperty(interfaceServer, defaultValue).equals(enabledValue);
				} else {
					new File(path).mkdirs();
					configFile.createNewFile();
					AppConfig.isOpServerEnabled = AppConfig.isAgServerEnabled = AppConfig.isMeServerEnabled = AppConfig.isRetailServerEnabled = AppConfig.isTelebetServerEnabled = AppConfig.isAPGServiceEnabled = AppConfig.isInterfaceServerEnabled = defaultValue
							.equals(enabledValue);
				}
			}

			// Store config values
			props.put(opServer, AppConfig.isOpServerEnabled ? enabledValue : disabledValue);
			props.put(agServer, AppConfig.isAgServerEnabled ? enabledValue : disabledValue);
			props.put(meServer, AppConfig.isMeServerEnabled ? enabledValue : disabledValue);
			props.put(retailServer, AppConfig.isRetailServerEnabled ? enabledValue : disabledValue);
			props.put(telebetServer, AppConfig.isTelebetServerEnabled ? enabledValue : disabledValue);
			props.put(apgService, AppConfig.isAPGServiceEnabled ? enabledValue : disabledValue);
			props.put(interfaceServer, AppConfig.isInterfaceServerEnabled ? enabledValue : disabledValue);

			configOutputStream = new FileOutputStream(configFile);
			props.store(configOutputStream, comment);
		} catch (IOException e) {
			AppUtility.recordException(e);
		} finally {
			if (configInputStream != null) {
				try {
					configInputStream.close();
				} catch (IOException e) {
					AppUtility.recordException(e);
				}
			}
			if (configOutputStream != null) {
				try {
					configOutputStream.close();
				} catch (IOException e) {
					AppUtility.recordException(e);
				}
			}
		}
	}

}
